package com.exp2.caloriedaily.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.exp2.caloriedaily.R
import com.exp2.caloriedaily.domain.CalorieData

class CalorieListAdapter(private val context: Context,private val calorieList:List<CalorieData>,private val background:Int):BaseAdapter() {
    override fun getCount(): Int =calorieList.size
    override fun getItem(position: Int): Any =calorieList[position]
    override fun getItemId(position: Int): Long =position.toLong()
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var view=convertView
        val holder: ViewHolder
        if (convertView==null){
            view=LayoutInflater.from(context).inflate(R.layout.calorie_item,null)
            holder= ViewHolder()
            holder.tv_calorie=view.findViewById<TextView>(R.id.tv_calorie)
            holder.tv_date= view.findViewById<TextView>(R.id.tv_date)
            view.tag=holder
        }else{
            holder= view!!.tag as ViewHolder
        }

        var blank= "                     "
        var blank2="                   "
        var blank3="                 "
        val calorie=calorieList[position]
        var length= calorie.date!!.length
        if (length==8)
            holder.tv_calorie.text=blank+calorie.calorie.toString()
        else if (length==9)
            holder.tv_calorie.text=blank2+calorie.calorie.toString()
        else if (length==10)
            holder.tv_calorie.text=blank3+calorie.calorie.toString()
        holder.tv_date.text=calorie.date
        return view
    }

    inner class ViewHolder{
        lateinit var tv_date:TextView
        lateinit var tv_calorie:TextView
    }
}